﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.Core;
using Sector7.Framework.DataDictionary.Agents;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// DataGroupSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataGroupSummaryWindow : UserControl
    {
        private List<S7_DataGroup> m_dataGroups = null;

        public DataGroupSummaryWindow()
        {
            InitializeComponent();
        }

        private void GetData()
        {
            m_dataGroups = DataGroupServiceAgent.GetAll();

            ClientUtil.LoadItemsSource(dtgMain, m_dataGroups);
        }

        private void DoModify()
        {
            var selectedGroup = dtgMain.SelectedItem as S7_DataGroup;

            if (selectedGroup != null)
            {
                AddDataGroupWindow modifyWindow = new AddDataGroupWindow();
                modifyWindow.DataGroup = selectedGroup;
                modifyWindow.InitializeWindow(CrudType.Update);
                modifyWindow.AddSuccess += new EventHandler(addWindow_AddSuccess);

                modifyWindow.ShowDialog();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddDataGroupWindow addWindow = new AddDataGroupWindow();
            addWindow.InitializeWindow(CrudType.Create);
            addWindow.AddSuccess += new EventHandler(addWindow_AddSuccess);

            addWindow.ShowDialog();
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            DoModify();
        }

        void addWindow_AddSuccess(object sender, EventArgs e)
        {
            AddDataGroupWindow addWindow = sender as AddDataGroupWindow;
            var newDataGroup = addWindow.DataGroup;

            switch (addWindow.Crud)
            {
                case CrudType.Create:
                    m_dataGroups.Add(newDataGroup);

                    ClientUtil.LoadSelectedItem(dtgMain, newDataGroup);

                    addWindow.ResetWindow();
                    break;
                case CrudType.Update:
                    m_dataGroups.Find(delegate(S7_DataGroup __group)
                    {
                        if (__group.GroupId == newDataGroup.GroupId)
                        {
                            __group = newDataGroup;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    });
                    break;
                default:
                    break;
            }
        }

        private void dtgMain_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DoModify();
        }
    }
}
